
const path = require('path');
const chalk = require("chalk");
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ESLintPlugin = require("eslint-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const ProgressBarPlugin = require("progress-bar-webpack-plugin");
const isDev = process.env.NODE_ENV === "development";
module.exports = {
  entry: './src/index.js',
  output: {
    path:path.resolve(__dirname,'../dist'),
    filename: isDev?'js/[name].js' : 'js/[name].[contenthash:8].js',
    chunkFilename:isDev? 'js/[name].js' : "js/[name].[cotenthash:8].js",
    publicPath:"/",
    library:{
      type:"umd",
      // name:"demos"
    }
  },
  resolve: {
    extensions: ['.js', '.less', '.css'],
    alias: {
      '@':path.resolve(__dirname,'../src'),
    },
  },
  cache: {
    type: "filesystem", // 使用文件缓存
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
        },
      },
      {
        test:/\.(le|c)ss$/,
        use:[
          isDev ? 'style-loader': MiniCssExtractPlugin.loader,
          {
            loader: "css-loader",
            options: {
              importLoaders: 2,
              sourceMap:isDev,
              modules: {
                mode: "local",
                localIdentName: isDev?"[path][name]__[local]" : "[hash:base64:5]",
                auto: (resourcePath) => {
                  return !/(\/|\\)node_modules(\/|\\)|(\/|\\)src(\/|\\)assets/g.test(resourcePath);
                },
              },
            },
          },
          'postcss-loader',
          {
            loader:'less-loader',
            options:{
              lessOptions:{
                modifyVars: {
                  // hack: `true; @import "${path.resolve(__dirname,"../src/assets/style/theme.less")}";`,
                  "primaryColor":"#ccc"
                },
                javascriptEnabled: true,
              }
            }
          }
        ]
      },
      {
        test: /\.(png|jpe?g|gif|svg|eot|ttf|woff|woff2|mp3|mp4|web(m|p)|ogg|wav|flac|aac)$/i,
        type: "asset",
        generator: {
          filename: 'assets/[hash][ext][query]'
        },
        parser: {
          dataUrlCondition: {
            maxSize: 2 * 1024 
          }
        }
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
        template:path.resolve(__dirname,'../public/index.html')
    }),
    new ESLintPlugin(),
    new ProgressBarPlugin({
      format: `  :msg [:bar] ${chalk.green.bold(":percent")} (:elapsed s)`,
    }), 
  ],
}
